package com.hymall.service.wx;

import com.hymall.bean.*;
import com.hymall.bean.bo.*;
import com.hymall.bean.vo.*;
import com.hymall.mapper.MarketAddressMapper;
import com.hymall.mapper.MarketCartMapper;
import com.hymall.mapper.MarketOrderMapper;
import com.hymall.mapper.WxCartMapper;
import org.apache.commons.lang.RandomStringUtils;
import org.aspectj.weaver.ast.Var;
import org.omg.CORBA.TRANSACTION_MODE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.annotation.Target;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description 在service层实现wxcart模块
 * @Author 徐成喜
 * @Data 2022/7/19 14:32
 */
@Service
public class WxCartServiceImpl implements WxCartService {

    @Autowired
    MarketCartMapper marketCartMapper;

    @Autowired
    WxCartMapper wxCartMapper;

    @Autowired
    MarketAddressMapper marketAddressMapper;

    /**
     * @return 获得WxCartIndexVO对象
     * @description index模块
     */
    @Override
    public WxCartIndexVO wxCartIndex(Integer userId) {
        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();
        if(userId!=null) {
            criteria.andUserIdEqualTo(userId);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketCart> marketCarts = marketCartMapper.selectByExample(example);

        return WxCartTool.getCartIndexVO(marketCarts);

    }

    /**
     * @return 返回MarketCart对象，获取number
     * @description wxCartGoodscount模块，购物车商品数量
     */
    @Override
    public WxCartIndexVO wxCartGoodscount(Integer userId) {
        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();
        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        Boolean deleted = false;
        if(deleted!=true){
            criteria.andDeletedEqualTo(false);
        }
        List<MarketCart> marketCarts = marketCartMapper.selectByExample(example);
        if(marketCarts.isEmpty()){
            MarketCart marketCart = new MarketCart();
            WxCartIndexVO wxCartIndexVO = new WxCartIndexVO();
            WxCartTotalVO totalVO = new WxCartTotalVO();
            totalVO.setGoodsCount(0);
            wxCartIndexVO.setCartTotal(totalVO);
            return wxCartIndexVO;
        }
        return WxCartTool.getCartIndexVO(marketCarts);

    }

    /**
     * @return 购物车数量
     * @description wxCartAdd模块，加入购物车
     */
    @Override
    public WxCartIndexVO wxCartAdd(Integer userId, WxCartAddBO wxCartAddBO) {

        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();

        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(wxCartAddBO.getGoodsId()!=null){
            criteria.andGoodsIdEqualTo(wxCartAddBO.getGoodsId());
        }
        if(wxCartAddBO.getProductId()!=null){
            criteria.andProductIdEqualTo(wxCartAddBO.getProductId());
        }
        Boolean deleted = false;
        if(deleted!=true){
            criteria.andDeletedEqualTo(false);
        }
        List<MarketCart> marketCarts = marketCartMapper.selectByExample(example);
        //判断添加商品是否存在cart表
        if(marketCarts.isEmpty()){
            //不存在insert
            MarketGoods marketGoods = wxCartMapper.selectGoodsById(wxCartAddBO.getGoodsId());
            MarketGoodsProduct product = wxCartMapper.selectGoodsProductById(wxCartAddBO.getProductId());
            Date date = new Date();
            wxCartMapper.insertCart(date,deleted,userId,marketGoods,product,wxCartAddBO.getNumber());
            MarketCartExample example1 = new MarketCartExample();
            MarketCartExample.Criteria criteria1 = example1.createCriteria();
            if(userId!=null){
                criteria1.andUserIdEqualTo(userId);
            }
            if(deleted!=true){
                criteria1.andDeletedEqualTo(false);
            }
            List<MarketCart> marketCart = marketCartMapper.selectByExample(example1);
            return WxCartTool.getCartIndexVO(marketCart);
        }
        //存在，合并，+number
        short number = (short) (marketCarts.get(0).getNumber()+wxCartAddBO.getNumber());
        MarketCart cart = new MarketCart();
        cart.setNumber(number);
        marketCartMapper.updateByExampleSelective(cart,example);
        MarketCartExample example1 = new MarketCartExample();
        MarketCartExample.Criteria criteria1 = example1.createCriteria();
        if(userId!=null){
            criteria1.andUserIdEqualTo(userId);
        }
        if(deleted!=true){
            criteria1.andDeletedEqualTo(false);
        }
        List<MarketCart> marketCarts1 = marketCartMapper.selectByExample(example1);
        return WxCartTool.getCartIndexVO(marketCarts1);
    }

    /**
     * @return 获得WxCartIndexVO对象
     * @description 购物车筛选
     */
    @Override
    public WxCartIndexVO wxCartChecked(Integer userId, WxCartIsCheckBO checkBO) {
        List<Integer> productIds = checkBO.getProductIds();
        Boolean deleted = false;
        for (Integer productId : productIds) {
            MarketCartExample example = new MarketCartExample();
            MarketCartExample.Criteria criteria = example.createCriteria();
            if(userId!=null) {
                criteria.andUserIdEqualTo(userId);
            }
            if(productId!=null){
                criteria.andProductIdEqualTo(productId);
            }

            if(deleted!=true){
                criteria.andDeletedEqualTo(false);
            }
            MarketCart marketCart = new MarketCart();
            if(checkBO.getIsChecked()==1){
                marketCart.setChecked(true);
            }else {
                marketCart.setChecked(false);
            }
            marketCartMapper.updateByExampleSelective(marketCart,example);
        }


        MarketCartExample example1 = new MarketCartExample();
        MarketCartExample.Criteria criteria1 = example1.createCriteria();
        if(userId!=null) {
            criteria1.andUserIdEqualTo(userId);
        }
        if(deleted!=true){
            criteria1.andDeletedEqualTo(false);
        }
        List<MarketCart> marketCarts = marketCartMapper.selectByExample(example1);

        return WxCartTool.getCartIndexVO(marketCarts);
    }

    /**
     * @return a
     * @description 实现修改购物车核心逻辑
     */
    @Override
    public void wxCartUpdate(Integer userId, WxCartUpdateBO updateBO) {

        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();

        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(updateBO.getGoodsId()!=null){
            criteria.andGoodsIdEqualTo(updateBO.getGoodsId());
        }
        if(updateBO.getProductId()!=null){
            criteria.andProductIdEqualTo(updateBO.getProductId());
        }
        if(updateBO.getId()!=null){
            criteria.andIdEqualTo(updateBO.getId());
        }
        Boolean deleted = false;
        if(deleted!=true){
            criteria.andDeletedEqualTo(false);
        }
        MarketCart marketCart = new MarketCart();
        marketCart.setNumber(updateBO.getNumber());
        marketCartMapper.updateByExampleSelective(marketCart,example);
    }

    /**
     * @return 返回新的购物车数据
     * @description 删除购物车选中数据
     */
    @Override
    public WxCartIndexVO wxCartDelete(Integer userId, WxCartProductBO productBO) {
        MarketCartExample example1 = new MarketCartExample();
        MarketCartExample.Criteria criteria1 = example1.createCriteria();
        if(userId!=null) {
            criteria1.andUserIdEqualTo(userId);
        }
        Boolean deleted = false;
        if(deleted!=true){
            criteria1.andDeletedEqualTo(false);
        }
        if(productBO.getProductIds()!=null){
            for (Integer productId : productBO.getProductIds()) {
                criteria1.andProductIdEqualTo(productId);
                marketCartMapper.deleteByExample(example1);
            }
        }
        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();
        if(userId!=null) {
            criteria.andUserIdEqualTo(userId);
        }
        if(deleted!=true){
            criteria.andDeletedEqualTo(false);
        }
        List<MarketCart> marketCarts = marketCartMapper.selectByExample(example);

        return WxCartTool.getCartIndexVO(marketCarts);
    }

    /**
     * @return a
     * @description 立即下单
     */
    @Override
    public MarketCart wxCartFastadd(Integer userId, WxCartAddBO wxCartAddBO) {
        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();

        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(wxCartAddBO.getGoodsId()!=null){
            criteria.andGoodsIdEqualTo(wxCartAddBO.getGoodsId());
        }
        if(wxCartAddBO.getProductId()!=null){
            criteria.andProductIdEqualTo(wxCartAddBO.getProductId());
        }
        if(wxCartAddBO.getNumber()!=null){
            int l = wxCartAddBO.getNumber();
            criteria.andNumberEqualTo(((short) l));
        }
        Boolean deleted=true;
        if(deleted!=false){
            criteria.andDeletedEqualTo(true);
        }
        List<MarketCart> marketCarts = marketCartMapper.selectByExample(example);
        //判断添加商品是否存在cart表
        if(marketCarts.isEmpty()){
            //不存在insert
            MarketGoods marketGoods = wxCartMapper.selectGoodsById(wxCartAddBO.getGoodsId());
            MarketGoodsProduct product = wxCartMapper.selectGoodsProductById(wxCartAddBO.getProductId());
            Date date = new Date();
            wxCartMapper.insertCartFastAdd(date,deleted,userId,marketGoods,product,wxCartAddBO.getNumber());
            List<MarketCart> marketCart = marketCartMapper.selectByExample(example);

            return marketCart.get(0);
        }
        //存在，返回cart的id
        return marketCarts.get(0);
    }

    /**
     * @return 返回WxCartCheckoutVO对象
     * @description 购物车下单页面模块的实现
     */


    @Override
    public WxCartCheckoutVO wxCartCheckout(Integer userId, WxCartCheckoutBO checkoutBO) throws Exception {
        WxCartJudgeVO judge = wxCartMapper.selectCartDeletedById(checkoutBO.getCartId());
        //地址
        MarketAddress address;
        if(checkoutBO.getAddressId()==null){
            address=null;
        }
        if(checkoutBO.getAddressId()==0){
            checkoutBO.setAddressId(wxCartMapper.selectAddressIsDefaultId(userId));
        }
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(checkoutBO.getAddressId()!=null){
            criteria.andIdEqualTo(checkoutBO.getAddressId());
        }
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(example);
        if(!marketAddresses.isEmpty()){
            address = marketAddresses.get(0);
        }else {
            address =null;
        }

        //availableCouponLength
        Integer availableCouponLength = wxCartMapper.selectCartAvailableCoupon(userId);

        //价格
        //checkedGoodsList
        BigDecimal goodsTotalPrice=BigDecimal.ZERO;
        List<MarketCart> checkedGoodsList = new ArrayList<>();
        if(checkoutBO.getCartId()==0){
            checkedGoodsList = wxCartMapper.selectCheckedGoods(userId);
            goodsTotalPrice = wxCartMapper.selectCheckedGoodsSumPrice(userId);
        } else{
            checkedGoodsList = wxCartMapper.selectGoodsByCartId(userId,checkoutBO.getCartId());
            goodsTotalPrice= wxCartMapper.selectFastAddGoodsSumPrice(userId,checkoutBO.getCartId());
        }

        BigDecimal couponPrice=BigDecimal.ZERO;
        BigDecimal actualPrice=BigDecimal.ZERO;
        if(checkoutBO.getCouponId() > 0){
            couponPrice = wxCartMapper.selectDiscountByCouponId(checkoutBO.getCouponId());

        }else {
            couponPrice = wxCartMapper.selectDiscountByCouponIdZero(userId);
        }
        actualPrice = goodsTotalPrice.subtract(couponPrice);

        //运费
        BigDecimal middle = new BigDecimal(300);
        BigDecimal freightPrice = BigDecimal.ZERO;
        if(actualPrice.compareTo(middle)==1||actualPrice.compareTo(middle)==0){
            freightPrice=new BigDecimal(0);
        }else {
            freightPrice=new BigDecimal(18);
        actualPrice=freightPrice.add(actualPrice);
    }


        WxCartCheckoutVO wxCartCheckoutVO =
                new WxCartCheckoutVO(actualPrice, checkoutBO.getAddressId(),
                availableCouponLength, checkoutBO.getCartId(), address, checkedGoodsList, checkoutBO.getCouponId(),
                couponPrice, freightPrice, goodsTotalPrice, couponPrice, checkoutBO.getGrouponRulesId(),
                actualPrice, checkoutBO.getUserCouponId());
        return wxCartCheckoutVO;
    }



}
